/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.manager.model;

import com.tamnhat.entity.Item;
import com.tamnhat.entity.Partner;
import com.telsoft.applet.util.StringUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class PartnerModel {

    public static ArrayList<Partner> getAll(String condition) throws Exception {
        ArrayList<Partner> list = new ArrayList<Partner>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "select partner_id,partner_code,partner_name,status,to_char(create_date,'dd-mm-yyyy') from partners where 1 = 1 " + condition;
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                Partner p = new Partner();
                p.setId(rs.getString(1));
                p.setCode(rs.getString(2));
                p.setName(rs.getString(3));
                p.setStatus(rs.getString(4));
                p.setCreateDate(rs.getString(5));
                list.add(p);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }

    public static boolean insert(Partner p) throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        try {
            cn = AppServer.getConnection();
            String id = Database.getSequenceValue(cn, "seq_common");
            p.setId(id);
            Date d = new Date();
            p.setCreateDate(StringUtil.format(d, "dd-MM-yyyy"));
            String strSQL = " insert into partners "
                    + " values(?,?,?,?,to_date(?,'dd-mm-yyyy')) ";
            pst = cn.prepareStatement(strSQL);
            
            pst.setInt(1, Integer.parseInt(p.getId()));
            pst.setString(2, p.getCode());
            pst.setString(3, p.getName());
            pst.setInt(4, Integer.parseInt(p.getStatus()));
            pst.setString(5, p.getCreateDate());
            
            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }

    public static boolean remove(String id) throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;

        if (id != null || !id.equalsIgnoreCase("")) {
            String strSQL = " delete from partners where partner_id = " + id + " ";
            try {
                cn = AppServer.getConnection();
                stmt = cn.prepareStatement(strSQL);
                return stmt.executeUpdate() > 0;                
            } finally {
                Database.closeObject(stmt);
                Database.closeObject(cn);
            }
        }
        return false;
    }

    public static boolean update(Partner p) throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        try {
            cn = AppServer.getConnection();
            String strSQL = " update partners"
                    + " set partner_code = ?,partner_name = ?, status =? "
                    + " where partner_id = ? ";
            pst = cn.prepareStatement(strSQL);
            
            pst.setString(1, p.getCode());
            pst.setString(2, p.getName());
            pst.setInt(3, Integer.parseInt(p.getStatus()));
            pst.setInt(4, Integer.parseInt(p.getId()));
            
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
}
